package com.example.demo2_review.thread_safe_gather;

import java.util.*;

/**
 * @Author zwp14
 * @create 2024/6/18 14:59
 * @description 线程安全的集合类-TreeMap和linkedHashMap
 */
public class TreeMapAndLinkedHashMap {

    public static void main(String[] args) {
        //基于红黑树实现的有序Map
        Map<String,Object> treeMap = new TreeMap<>();
        treeMap.put("key3",33);
        treeMap.put("key2",22);
        treeMap.put("key1",66);
        treeMap.put("key4",44);
        treeMap.putIfAbsent("key1",11);//如果这个key的value是null则插入新的value如果不是则不进行操作
        System.out.println("treeMap.containsValue(\"key1\") = " + treeMap.containsValue(11));
        System.out.println("treeMap.containsValue(\"key1\") = " + treeMap.containsValue(22));
        System.out.println("treeMap.containsKey(\"key1\") = " + treeMap.containsKey("key1"));
        System.out.println("treeMap.containsKey(\"key1\") = " + treeMap.containsKey("key2"));
        System.out.println("treeMap.values() = " + treeMap.values());
        System.out.println("treeMap.keySet() = " + treeMap.keySet());
        System.out.println("treeMap.getOrDefault(\"key5\",55) = " + treeMap.getOrDefault("key5", 55));
        treeMap.entrySet().forEach(System.out::println);
        System.out.println("treeMap = " + treeMap);
        //Linked Hash Map-基于哈希表和双向链表实现的有序Map(有序指保持插入顺序)
        Map<String,Object> linkedHashMap = new LinkedHashMap<>(3);
        linkedHashMap.putIfAbsent("key1","value1");
        linkedHashMap.put("key2","value2");
        linkedHashMap.put("key3","value3");
        linkedHashMap.put("key0","value0");
        System.out.println("linkedHashMap = " + linkedHashMap);
    }
}
